<template>
  <div class="chart-container" ref="chartRef"></div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref(null)
let chartInstance = null

onMounted(() => {
  nextTick(() => {
    initChart()
    window.addEventListener('resize', handleResize)
  })
})

const initChart = () => {
  chartInstance = echarts.init(chartRef.value)

  const option = {
    title: {
      text: '患者治疗时间时长统计',
      left: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      data: [
        '0-15分钟',
        '16-30分钟',
        '31-45分钟',
        '46-60分钟',
        '60分钟以上'
      ]
    },
    series: [
      {
        name: '治疗时长',
        type: 'pie',
        radius: '55%',
        center: ['50%', '60%'],
        data: [
          { value: 335, name: '0-15分钟' },
          { value: 310, name: '16-30分钟' },
          { value: 234, name: '31-45分钟' },
          { value: 135, name: '46-60分钟' },
          { value: 1548, name: '60分钟以上' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }

  chartInstance.setOption(option)
}

const handleResize = () => {
  chartInstance && chartInstance.resize()
}

// const onUnmounted = () => {
//   window.removeEventListener('resize', handleResize);
//   chartInstance && chartInstance.dispose();
// };
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 300px;
}
</style>
